import java.util.Arrays;
import java.util.PriorityQueue;

public class Demo {
    private static volatile int COUNTER = 0;
    private static Object lock = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (COUNTER % 3 != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print("A");
                    COUNTER++;
                    lock.notifyAll();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (COUNTER % 3 != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print("B");
                    COUNTER++;
                    lock.notifyAll();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (COUNTER % 3 != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("C");
                    COUNTER++;
                    lock.notifyAll();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
class Solution {
    public int scheduleCourse(int[][] courses) {
        Arrays.sort(courses,(a, b)->a[1]-b[1]);
        PriorityQueue<Integer> p = new PriorityQueue<>((a,b)->b-a);
        int day = 0;
        for(int[] x : courses){
            int i=x[0], j=x[1];
            if(day+i <= j){
                day += i;
                p.offer(i);
            }else if(!p.isEmpty() && i < p.peek()){
                day -= p.poll()-i;
                p.offer(i);
            }
        }
        return p.size();
    }
}